package gamecode;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements KeyListener, MouseListener {

	private boolean loaded = false;
	private boolean inGame = false;
	private boolean mousePressed = false;
	
	EntityPlayerShip player;
	EntityManager emanager;
	SpaceBackground spacebg;
	
	public GamePanel() {
		super();
		
		// Key bindings -- not working at the moment
		/*this.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "pressedLeft");
		this.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "pressedRight");
		this.getActionMap().put("pressedLeft", new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(inGame) {
					player.rotateLeft();
				}
			}
		});
		this.getActionMap().put("pressedRight", new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(inGame) {
					player.rotateRight();
				}
			}
		});*/
		
		this.addMouseListener(this);
		this.addKeyListener(this);
	}
	
	private void loadComponents() {
		this.setBackground(Color.BLACK);
		this.setFocusable(true);
		this.requestFocus();
		player = new EntityPlayerShip();
		player.setPosition(10, 10);
		player.setVelocity(30, 1);
		player.rotate(30);
		emanager = new EntityManager();
		emanager.addEntity(new EntityDebugBouy(Color.RED, new DPoint(40, 40)));
		emanager.addEntity(new EntityDebugBouy(Color.BLUE, new DPoint(600, 500)));
		emanager.addEntity(new EntityDebugBouy(Color.WHITE, new DPoint(900, 400)));
		spacebg = new SpaceBackground(40, this.getBounds(), player);
	}
	
	public void keyPressed(KeyEvent e) {
		if(inGame) {
			player.keyPressed(e);
		}
		else {
			// Pass event through to menu
		}
	}
	
	public void keyReleased(KeyEvent e) {
		if(inGame) {
			player.keyReleased(e);
		}
	}
	
	public void keyTyped(KeyEvent e) { }
	
	public void mousePressed(MouseEvent e) {
		if(inGame && !mousePressed) {
			emanager.addEntity(new EntityDebugBouy(Color.YELLOW, new DPoint(e.getX()+player.getPosition().getX()-this.getBounds().width/2, e.getY()+player.getPosition().getY()-this.getBounds().height/2)));
		}
		mousePressed = true;
	}
	
	public void mouseReleased(MouseEvent e) {
		mousePressed = false;
	}
	
	public void mouseClicked(MouseEvent e) { }
	public void mouseEntered(MouseEvent e) { }
	public void mouseExited(MouseEvent e) { }
	
	public void paint(Graphics g) {
		if(loaded) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			spacebg.drawStars(g2d, this.getBounds(), player);
			emanager.drawEntities(g2d, this.getBounds(), player);
			player.drawShip(g2d, this.getBounds());
			g2d.drawString(String.valueOf(player.getDirection()), 10, 10);
			player.update();
			try {
				Thread.sleep(10);
			}
			catch (Exception ex) {
			}
			this.repaint();
		}
		else {
			loadComponents();
			loaded = true;
			inGame = true;
		}
	}
}